/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source: any) {
  if (!source && typeof source !== 'object') {
    // @ts-ignore
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj: any = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

// 获取多久之前
// @param {string} time 2020-12-12 12:12:12
// @returns {string}
export function timeAgo(time: string): string {
  const inputDate = new Date(time)
  const currentDate = new Date()
  const diffTime = Math.abs(currentDate.getTime() - inputDate.getTime())
  const diffMonths = Math.floor(diffTime / (1000 * 60 * 60 * 24 * 30))

  if (diffMonths > 1) {
    return inputDate.toLocaleString()
  }

  const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24))
  const diffHours = Math.floor(diffTime / (1000 * 60 * 60))
  const diffMinutes = Math.floor(diffTime / (1000 * 60))

  if (diffDays > 30) {
    return time
  } else if (diffDays > 0) {
    return `${diffDays}天前`
  } else if (diffHours > 0) {
    return `${diffHours}小时前`
  } else if (diffMinutes > 0) {
    return `${diffMinutes}分钟前`
  } else {
    return `刚刚`
  }
}

// 获取多久之前
// @param {string} time 2020-12-12 12:12:12
// @returns {string}
export function timeAgoTwo(time: string): string {
  const inputDate = new Date(time)
  const currentDate = new Date()
  const diffTime = Math.abs(currentDate.getTime() - inputDate.getTime())
  const diffMonths = Math.floor(diffTime / (1000 * 60 * 60 * 24 * 30))

  if (diffMonths > 1) {
    const year = inputDate.getFullYear()
    const currentYear = currentDate.getFullYear()
    if (year === currentYear) {
      return inputDate
        .toLocaleString()
        .split(' ')[0]
        .split('/')
        .slice(1, 3)
        .join('/')
    } else {
      return inputDate.toLocaleString().split(' ')[0]
    }
  }

  const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24))
  const diffHours = Math.floor(diffTime / (1000 * 60 * 60))
  const diffMinutes = Math.floor(diffTime / (1000 * 60))

  if (diffDays > 30) {
    const arr = time.split(' ')
    return arr[0]
  } else if (diffDays > 0) {
    return `${diffDays}天前`
  } else if (diffHours > 0) {
    return `${diffHours}小时前`
  } else if (diffMinutes > 0) {
    return `${diffMinutes}分钟前`
  } else {
    return `刚刚`
  }
}

export function isWithin30Days(dateString: string): boolean {
  const givenDate = new Date(dateString)
  const currentDate = new Date()
  const timeDifference = Math.abs(currentDate.getTime() - givenDate.getTime())
  const daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24))
  return daysDifference <= 30
}

export function getUrlParams(): object {
  const params = {}
  const queryString = window.location.search.substring(1)
  const regExp = /([^&=]+)=([^&]*)/g
  let match

  while ((match = regExp.exec(queryString))) {
    params[decodeURIComponent(match[1])] = decodeURIComponent(match[2])
  }
  return params
}
